<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\Person;

class UpdatePersonReuqest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->input('id');
        $person = Person::find($id);
        if (! $person) {
            return response(view('errors.404'), 404);
        }

        $validate_rules = [
            //'avatar' => 'required',
            'name' => 'required|max:50',
            'id_number' => [
                'required',
                'max:50',
                Rule::unique('persons')->ignore($this->id),
            ],
            'reg_addr' => 'required|max:50',
            'temp_addr' => 'required|max:50',
            //'five_saw_time' => 'date|after_or_equal:' . date('Y-m-d', $person->five_saw_time),
            'meet_situation' => 'max:255',
            'remark' => 'max:2000',
            'phone' => 'required|max:50',
            'qq' => 'max:50',
            'weixin' => 'max:50',
        ];

        $this->five_saw_time_raw = date('Y-m-d', $person->five_saw_time);

        // 来我市时间只有不为空时才验证
        if (!empty($this->five_saw_time)) {
            $validate_rules['five_saw_time'] = 'date|after:' . date('Y-m-d', $person->five_saw_time);
            // 离开时间只有都不为空时才验证
            if (!empty($this->leave_time)) {
                $validate_rules['leave_time'] = 'date|after:five_saw_time';
            }
        }

        return $validate_rules;
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'id_number.unique' => '身份证号重复',
            'meet_situation.max' => '见面情况不能超过255个字符',
            'remark.max' => '我市活动情况不能超过2000个字符',
            'leave_time.after_or_equal' => '离开时间不能早于到我市时间',
            'five_saw_time.after_or_equal' => '来我市时间不能小于' . date('Y-m-d', $this->five_saw_time),
            'leave_time.after' => '离开时间必须大于来我市时间',
        ];
    }
}
